package com.example.demo.util;

import com.example.demo.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: huanggaotao
 * @Description:
 * @Date: Created in 14:53 2021/7/29
 * @Modified By:
 */
public class HtmlParseUtil {

  public List<Content> parseJD(String keywords) throws IOException {
      //获取请求   https://search.jd.com/Search?keyword=java
      //前提，需要联网
      String url = "https://search.jd.com/Search?keyword="+keywords;
      Document document = Jsoup.parse(new URL(url), 30000);
      Element element = document.getElementById("J_goodsList");
      //获取这个div下的所有li标签
      Elements elements = document.getElementsByTag("li");
      List<Content> contents = new ArrayList<>();
      for (Element el:elements){
          if (el.attr("class").equalsIgnoreCase("gl-item")){
              //获取当前商品的图片
              //关于这种图片特别多的网站，所有的图片都是延迟加载的
              String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
              //获取当前商品的价格
              String price = el.getElementsByClass("p-price").eq(0).text();
              //获取当前商品的标题
              String title = el.getElementsByClass("p-name").eq(0).text();
//              System.out.println("============================");
//              System.out.println(img);
//              System.out.println(price);
//              System.out.println(title);
              Content content = new Content();
              content.setTitle(title);
              content.setImg(img);
              content.setPrice(price);
              contents.add(content);

          }
      }
      return contents;
  }
}
